<template>
  <a-modal class="common-modal" :title="$t('other.copyTo')" :footer="null" :width="600" :visible="visible" @cancel="handleCancel" :maskClosable="false" style="top:5%;">
    <a-spin :spinning="confirmLoading">
      <div class="p-20">
        <a-form layout="inline">
          <a-form-item :label="$t('hangManage.makeNo')">
            <a-input v-model:value.trim="queryParam.moNo" :placeholder="$t('other.pleaseInputMakeNo')" @keyup.enter="loadData" allowClear />
          </a-form-item>
          <a-form-item>
            <a-space>
              <a-button type="primary" @click="loadData">{{$t('common.see')}}</a-button>
              <a-button @click="searchReset">{{$t('common.reset')}}</a-button>
            </a-space>
          </a-form-item>
        </a-form>
        <a-table class="pt-20" :loading="loading" rowKey="id" bordered size="small" :dataSource="dataSource" :columns="columns" :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :pagination="ipagination" @change="handleTableChange" :scroll="{y: 600}">
        </a-table>
      </div>
      <div class="common-modal-footer">
        <a-button @click="handleCancel" class="cancel">{{$t('common.cancel')}}</a-button>
        <a-button @click="handleOk" type="primary" class="confirm">{{$t('common.ok')}}</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { ListMixinPm } from '@/mixin/listMixinPm.js'
import { copyColumns } from './routeChartConfig'
import axios from '@/utils/request'
export default {
  name: 'copyRouteModal',
  mixins: [ListMixinPm],
  data () {
    return {
      columns: copyColumns,
      visible: false,
      confirmLoading: false,
      queryParam: {},
      url: {
        list: '/api/mo/mo/page',
        routeUrl: '/api/mo/mo-proc-roadmap/cope',
        procUrl: '/api/mo/mo-proc/cope'
      },
      ipagination: {
        current: 1,
        pageSize: 10,
        total: 0
      },
      versionId: null,
      isRoute: false,
      showMore: true
    }
  },
  methods: {
    async open (id, isRoute) {
      this.versionId = id
      this.isRoute = isRoute
      this.queryParam = {}
      await this.loadData()
      this.visible = true
    },
    async handleOk () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning(this.$t('other.pleaseSelectMake'))
        return
      }
      let url = this.isRoute ? this.url.routeUrl : this.url.procUrl
      let params = this.isRoute ? {
        moIdList: this.selectedRowKeys,
        moProcRoadmapId: this.versionId
      } : {
        moIdList: this.selectedRowKeys,
        moProcId: this.versionId
      }
      try {
        const res = await axios.postAction(url, params)
        this.confirmLoading = false
        if (res.success) {
          this.$emit('ok', params)
          this.visible = false
        }
      } catch (err) {
        this.confirmLoading = false
      }
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
    }
  }
}
</script>
